package com.zsrt.day20170424.homework;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.CharBuffer;

import org.junit.Test;

/**
*@author jzh
*@创建时间 2017年4月24日下午6:32:43
*类说明：
*/
public class IOTest {
	
	@Test
	public void charTxtCopy(){
		File file1=new File("iotest/aaa.txt");
		File file2=new File("iotest/bbb.txt");
		
		Reader fr=null;
		Writer fw=null;
		try {
			fr = new FileReader(file1);
			fw = new FileWriter(file2);
			char[] b=new char[3];
			int len=0;
			while((len=fr.read(b))!=-1){
				fw.write(b, 0, len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(fr!=null){
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fw!=null){
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}
	@Test
	public void byteTxtCopy(){
		File file1=new File("iotest/aa.txt");
		File file2=new File("iotest/bb.txt");
		
		InputStream is=null;
		OutputStream os=null;
		try {
			is = new FileInputStream(file1);
			os = new FileOutputStream(file2);
			
			byte[] bs=new byte[2];
			int len=0;
			while((len=is.read(bs))!=-1){
				String string=new String(bs, 0, len);
				System.out.println(string);
				os.write(bs, 0, len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			try {
				if(is!=null){
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(os!=null){
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	@Test
	public void bytePictureCopy(){
		File file1=new File("iotest/demo01.png");
		File file2=new File("iotest/demo02.png");
		
		InputStream is=null;
		OutputStream os=null;
		try {
			is = new FileInputStream(file1);
			os = new FileOutputStream(file2);
			
			byte[] bs=new byte[2];
			int len=0;
			while((len=is.read(bs))!=-1){
				os.write(bs, 0, len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			try {
				if(is!=null){
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(os!=null){
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Test
	public void fileWriterTest(){
		File file=new File("iotest/cc.txt");
		Writer w=null;
		try {
			w = new FileWriter(file);
			w.write("中深软通123");
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(w!=null){
					w.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	@Test
	public void bufferedWriterTest(){
		File file=new File("iotest/BufferedWriter.txt");
		BufferedWriter bw=null;
		try {
			Writer w=new FileWriter(file);
			bw = new BufferedWriter(w);
			bw.write("中深软通123");
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(bw!=null){
					bw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Test
	public void outputStreamWriterTest(){
		File file=new File("iotest/OutputStreamWriter.txt");
		OutputStreamWriter osw=null;
		try {
			OutputStream os=new FileOutputStream(file);
			osw = new OutputStreamWriter(os);
			osw.write("中深软通123");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(osw!=null){
					osw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

}
